package com.ruoyi.dynamic.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.CollectionUtils;
import com.ruoyi.dynamic.mapper.RoomInteractMapper;
import com.ruoyi.dynamic.model.constant.DynamicConstant;
import com.ruoyi.dynamic.model.constant.RoomChatRoomKvConstant;
import com.ruoyi.dynamic.model.pojo.entity.Gift;
import com.ruoyi.dynamic.model.pojo.entity.Room;
import com.ruoyi.dynamic.model.pojo.entity.RoomInteract;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomInteractAddRequest;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomUserSearchRequest;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomWishCloseRequest;
import com.ruoyi.dynamic.service.IGiftService;
import com.ruoyi.dynamic.service.IMessageService;
import com.ruoyi.dynamic.service.IRoomInteractService;
import com.ruoyi.dynamic.service.IRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 房间互动挑战 服务实现类
 * </p>
 *
 * @author Gnc
 * @since 2025-01-13
 */
@Service
public class RoomInteractServiceImpl extends ServiceImpl<RoomInteractMapper, RoomInteract> implements IRoomInteractService {

    @Autowired
    private IRoomService iRoomService;
    @Autowired
    private IGiftService iGiftService;
    @Autowired
    private IMessageService iMessageService;

    @Override
    public boolean add(AppRoomInteractAddRequest request, Long userId) {
        Room verify = iRoomService.verify(request.getRoomId());
        Gift gift = iGiftService.verify(request.getGiftId());

        RoomInteract roomInteract = new RoomInteract();
        roomInteract.setGiftId(request.getGiftId());
        roomInteract.setGiftImgUrl(gift.getGiftImg());
        roomInteract.setGiftPrice(gift.getGiftPrice());
        roomInteract.setUserId(userId);
        roomInteract.setRoomId(request.getRoomId());
        roomInteract.setCreateTime(new Date());
        roomInteract.setStatus(DynamicConstant.CheckStatus.YES);
        roomInteract.setName(request.getName());
        return save(roomInteract);
    }

    @Override
    public boolean launch(AppRoomWishCloseRequest request, Long userId) {
        Room verify = iRoomService.verify(request.getRoomId());

        List<RoomInteract> list = lambdaQuery().eq(RoomInteract::getRoomId, request.getRoomId())
                .eq(RoomInteract::getStatus, DynamicConstant.CheckStatus.YES)
                .list();
        Assert.notEmpty(list, "互动挑战资源为空");

        iMessageService.setRoomChatRoomAttr(verify.getGroupId(), verify.getUserId().toString(),
                RoomChatRoomKvConstant.ROOM_INTERACT_INFO, JSONObject.toJSONString(list), 0, "", "");
        return true;
    }

    @Override
    public boolean over(AppRoomWishCloseRequest request, Long userId) {
        Room verify = iRoomService.verify(request.getRoomId());

        iMessageService.setRoomChatRoomAttr(verify.getGroupId(), verify.getUserId().toString(),
                RoomChatRoomKvConstant.ROOM_INTERACT_INFO, JSONObject.toJSONString(CollectionUtils.emptyList()), 0, "", "");
        return true;
    }

    @Override
    public List<RoomInteract> listByRoomId(AppRoomUserSearchRequest request, Long userId) {
        return lambdaQuery().eq(RoomInteract::getRoomId, request.getRoomId())
                .orderByAsc(RoomInteract::getCreateTime)
                .list();
    }
}
